package com.linshengjian.web.security;

import com.linshengjian.web.model.Result;
import com.linshengjian.web.utils.ResultUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class SecurityRestHandler extends SimpleUrlAuthenticationSuccessHandler implements AuthenticationEntryPoint, AuthenticationFailureHandler, LogoutSuccessHandler {

    private void header(HttpServletResponse response, Result.Code code, String msg) throws IOException {
        response.setStatus(HttpServletResponse.SC_OK);

        response.setHeader("content-type", "application/json;charset=UTF-8");

        Result result = ResultUtils.result(code, msg);
        PrintWriter writer = response.getWriter();
        writer.write(result.toString());
        writer.flush();
    }


    /**
     * 无权限
     * @param request
     * @param response
     * @param authException
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        header(response, Result.Code.NotPermission, "没有权限");
    }

    /**
     * 登录授权成功
     * @param request
     * @param response
     * @param authentication
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        clearAuthenticationAttributes(request);
        header(response, Result.Code.SUCCESS, "");
    }

    /**
     * 登录授权失败
     * @param request
     * @param response
     * @param exception
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        header(response, Result.Code.NotPermission, "授权失败");
    }

    /**
     * 注销
     * @param request
     * @param response
     * @param authentication
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        header(response, Result.Code.SUCCESS, "注销成功");
    }
}
